package com.auto.learning.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.activity.PlayActivity;
import com.auto.learning.bean.RemindBean;
import com.auto.learning.event.Handle;
import com.auto.learning.view.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class RemindAdapter extends BaseAdapter {

	private Context mContext;
	private List<RemindBean> mList;
	private DisplayImageOptions options;
	private Handle handle;

	public RemindAdapter(Context context, List<RemindBean> list,Handle handle) {
		this.mList = list;
		this.mContext = context;
		this.handle =handle;
		initImageLoader();
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return mList.get(position).getId();
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item_remind, null);
			viewHolder.civ_userface = (CircleImageView) convertView.findViewById(R.id.civ_userface);
			viewHolder.v_redpoint = convertView.findViewById(R.id.v_redpoint);
			viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
			viewHolder.tv_msg = (TextView) convertView.findViewById(R.id.tv_msg);
			viewHolder.tv_bookComment = (TextView) convertView.findViewById(R.id.tv_bookComment);
			viewHolder.tv_msgTag = (TextView) convertView.findViewById(R.id.tv_msgTag);
			viewHolder.tv_createTime = (TextView) convertView.findViewById(R.id.tv_createTime);
			viewHolder.tv_delete = (TextView) convertView.findViewById(R.id.tv_delete);
			viewHolder.tv_reply = (TextView) convertView.findViewById(R.id.tv_reply);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		RemindBean remindBean = mList.get(position);
		if (remindBean.getIsRead() == 1) {
			viewHolder.v_redpoint.setVisibility(View.VISIBLE);
		} else {
			viewHolder.v_redpoint.setVisibility(View.GONE);
		}
		if (remindBean.getType() == 2) {
			viewHolder.tv_reply.setVisibility(View.GONE);
			viewHolder.tv_msg.setVisibility(View.GONE);
		} else {
			viewHolder.tv_reply.setVisibility(View.VISIBLE);
			viewHolder.tv_msg.setVisibility(View.VISIBLE);
		}
		ImageLoader.getInstance().displayImage(remindBean.getFaceUrl(), viewHolder.civ_userface, options);
		viewHolder.tv_title.setText(remindBean.getTitle()+"");
		viewHolder.tv_msg.setText(remindBean.getMsg()+"");
		viewHolder.tv_bookComment.setText(remindBean.getBookComment()+"");
		viewHolder.tv_msgTag.setText(remindBean.getMsgTag()+"");
		viewHolder.tv_createTime.setText(remindBean.getCreateTime()+"");
		viewHolder.tv_delete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				handle.obtainMessage(1, position,0).sendToTarget();
			}
		});
		viewHolder.tv_reply.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				handle.obtainMessage(3, position,0).sendToTarget();
			}
		});
		viewHolder.tv_msgTag.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent	mIntent = new Intent(mContext, PlayActivity.class);
				mIntent.putExtra("bookId", mList.get(position).getBookId());
				mIntent.putExtra("faceImg", mList.get(position).getImgFace());
				mIntent.putExtra("type", 2);
				mContext.startActivity(mIntent);
			}
		});
		
		return convertView;
	}


	public class ViewHolder {
		CircleImageView civ_userface;
		View v_redpoint;
		TextView tv_title, tv_msg, tv_bookComment, tv_msgTag, tv_createTime,tv_delete, tv_reply;
	}

	private void initImageLoader() {
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.me_user)
				.showImageForEmptyUri(R.drawable.me_user)
				.showImageOnFail(R.drawable.me_user).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

}
